package com.elf.stepcounter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.elf.stepcounter.databinding.ActivityMainBinding;
import com.elf.zero.step.StepData;
import com.elf.zero.step.StepJobService;
import com.elf.zero.step.StepManger;
import com.elf.zero.step.StepService;
import com.elf.zero.utils.DeviceUtils;

import java.util.List;

public class MainActivity extends AppBaseActivity {

    private ActivityMainBinding binding;
    private BroadcastReceiver receiver;
    private StepManger stepManger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
//        hideHomeAsUp();
        if (getSupportActionBar() != null) {
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_home);
        }

        stepManger = new StepManger(this);

        StepService.startService(this);
        showData();
        register();

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            StepJobService.start(this);
        }

        String id = DeviceUtils.getDeviceId();
        showToast("id = " + id);
    }

    @Override
    protected int getMenuResId() {
        return R.menu.home;
    }

    @Override
    protected void menuSelected(MenuItem item) {
        super.menuSelected(item);
        if (item.getItemId() == R.id.menu_setting) {
            SettingActivity.open(this);
        }
    }

    private void showData() {
        StepData data = stepManger.getCurrentDayData();
        if (data == null) {
            binding.tvStepCount.setText("0");
            binding.tvKm.setText("0.00");
        } else {
            binding.tvStepCount.setText(String.valueOf(data.step));
            binding.tvKm.setText(StepManger.stepToKm(data.step));
        }

        List<StepData> list = stepManger.getLastWeekData();
        binding.layoutHistory.removeAllViews();
        if (list == null || list.isEmpty()) {
            binding.tvEmpty.setVisibility(View.VISIBLE);
            binding.layoutHistory.setVisibility(View.GONE);
        } else {
            binding.tvEmpty.setVisibility(View.GONE);
            binding.layoutHistory.setVisibility(View.VISIBLE);

            addViewToList(new StepDataListItemHeader(this));

            StepDataListItem listItem;
            for (StepData item : list) {
                listItem = new StepDataListItem(this);
                listItem.setData(item);
                addViewToList(listItem);
            }

//            addViewToList(new StepDataListItem(this));
        }
    }

    private void addViewToList(View view) {
        binding.layoutHistory.addView(view,
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    private void register() {
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                showData();
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(StepService.action_step_update);
        registerReceiver(receiver, filter);
    }

    private void unRegister() {
        try {
            if (receiver != null) {
                unregisterReceiver(receiver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unRegister();
        stepManger.destroy();
    }
}